package cn.fl.custpay.service.impl;

import java.util.*;

import cmm.comm.facade.livecheck.api.ICmmWhiteListApiService;
import cmm.mid.core.framework.page.KeyValueVO;
import cn.cmm.base.framework.utils.DateUtils;
import cn.fl.custpay.service.ICmmWhiteListService;
import cn.fl.custpay.transform.CmmWhiteListVOTransform;
import cn.fl.custpay.transform.CmsCmmCntDcVOTransform;
import cn.fl.custpay.vo.CmmWhiteListVO;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.framework.constant.DictionaryConstant;
import cn.fl.system.service.ISysDictionaryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cmm.comm.facade.livecheck.dto.CmmWhiteListDTO;
import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cmm.mid.core.framework.page.Pager;


/**
 * @Description: 客户白名单 客户端服务层接口实现
 * @Description: 业务层方法名称前缀：新增：add* 删除：remove* 修改：modify* 查询对象：select* get* 批量：batch* 查询集合：search*
 */
@Service
public class CmmWhiteListServiceImpl implements ICmmWhiteListService {

	/**
	 * 日志
	 */
	private static final Logger logger = LoggerFactory.getLogger(CmmWhiteListServiceImpl.class);
	
	/**
	 * 自定义 API 自行注入需要的API服务
	 */
	@Autowired
	private ICmmWhiteListApiService cmmWhiteListApiService;
	@Autowired
	private ISysDictionaryService sysDictionaryService;

	@Override
	public CmmWhiteListVO selectById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.info(logger, "selectById", "主键参数不能为空!", CurrentThreadContext.getCurrentUserName());
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "获取对象主键不能为空!");
		}
		//TODO 业务逻辑
		CmmWhiteListDTO dto = this.cmmWhiteListApiService.selectById(id);
		return CmmWhiteListVOTransform.toVO(dto);
	}

	@Override
	public Long add(CmmWhiteListVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.info(logger, "add", "新增对象不存在!");
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,"新增对象不存在!");
		};
		return this.cmmWhiteListApiService.add(CmmWhiteListVOTransform.toDTO(entity));
	}

	@Override
	public boolean removeById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.info(logger, "removeById", "删除对象主键不能为空!");
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,"删除对象主键不能为空!");
		};
		CmmWhiteListVO cmmWhiteListVO = CmmWhiteListVOTransform.toVO(this.cmmWhiteListApiService.selectById(id));
		if(null == cmmWhiteListVO){
			BaseLogUtils.info(logger, "removeById", "删除对象不存在!");
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,"删除对象不存在!");
		}
		cmmWhiteListVO.setIsDel(1);
		return this.modify(cmmWhiteListVO);
	}
	
	/**
     * 删除
     * @param ids
     * @return
     */
	@Override
    public boolean removeByIds(List<Long> ids)throws BaseException{
		if(CollectionUtils.isEmpty(ids)){
			BaseLogUtils.info(logger, "removeByIds", "删除主键参数集合不能为空!", CurrentThreadContext.getCurrentUserName());
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,"删除对象主键不能为空!");
		};
		return false;
    }

	@Override
	public boolean modify(CmmWhiteListVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.info(logger, "modify", "修改对象不能为空!");
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,"修改对象不能为空!");
		};
		return this.cmmWhiteListApiService.modify(CmmWhiteListVOTransform.toDTO(entity));
	}

	@Override
	public Pager searchListPage(Pager page) throws BaseException{
		if(null == page){
			page = new Pager();
		}
		Pager pager = this.cmmWhiteListApiService.searchCmmWhiteListListPage(page);
		List<CmmWhiteListDTO> list = (List<CmmWhiteListDTO>) pager.getResultList();
		//数据字典（系统名+是否启用）
		List<String> codeGrpCdList = new ArrayList<>();
		codeGrpCdList.add(DictionaryConstant.CODE_GRP_CERT_TYP_CD);
		codeGrpCdList.add(DictionaryConstant.CODE_GRP_LIV_STS_CD);
		codeGrpCdList.add(DictionaryConstant.CODE_GRP_LIV_TYP_CD);
		Map<String, List<KeyValueVO>> dicMap = this.sysDictionaryService.getMultiKvList(CurrentThreadContext.getFlServiceSysCd(), CurrentThreadContext.getCurrentOrgCd(), codeGrpCdList);
		List<KeyValueVO> sysList = dicMap.get(DictionaryConstant.CODE_GRP_CERT_TYP_CD);
		List<KeyValueVO> livstsList = dicMap.get(DictionaryConstant.CODE_GRP_LIV_STS_CD);
		List<KeyValueVO> livtypList = dicMap.get(DictionaryConstant.CODE_GRP_LIV_TYP_CD);
		Map<String, String> sysMap = null;
		if (CollectionUtils.isNotEmpty(sysList)) {
			sysMap = new HashMap<>(sysList.size());
			for (KeyValueVO dto : sysList) {
				sysMap.put(dto.getKey(), dto.getValue());
			}
		}
		Map<String, String> livstsMap = null;
		if (CollectionUtils.isNotEmpty(livstsList)) {
			livstsMap = new HashMap<>(livstsList.size());
			for (KeyValueVO dto : livstsList) {
				livstsMap.put(dto.getKey(), dto.getValue());
			}
		}
		Map<String, String> livtypMap = null;
		if (CollectionUtils.isNotEmpty(livtypList)) {
			livtypMap = new HashMap<>(livtypList.size());
			for (KeyValueVO dto : livtypList) {
				livtypMap.put(dto.getKey(), dto.getValue());
			}
		}
		List<CmmWhiteListVO> cmmWhiteListVOList = CmmWhiteListVOTransform.toVOList(list);
		for (CmmWhiteListVO cmmWhiteListVO : cmmWhiteListVOList) {
			if (sysMap != null && sysMap.containsKey(cmmWhiteListVO.getCertTypCd())) {
				cmmWhiteListVO.setCertTypCdNm(sysMap.get(cmmWhiteListVO.getCertTypCd()));
			}
			String isValid = DateUtils.before(cmmWhiteListVO.getEndTm(),new Date()) ? "1" : "2";
			if (livstsMap != null && livstsMap.containsKey(isValid)){
				cmmWhiteListVO.setIsValidNm(livstsMap.get(isValid));
			}
			if (livtypMap != null && livtypMap.containsKey(cmmWhiteListVO.getListTypCd())) {
				cmmWhiteListVO.setListTypCdNm(livtypMap.get(cmmWhiteListVO.getListTypCd()));
			}
		}
		pager.setResultList(cmmWhiteListVOList);
		return pager;
	}
}
